﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using XRoute.DataExchange;

namespace XRoute.Server.Services.Dex
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
                     ConcurrencyMode = ConcurrencyMode.Multiple)]
    class DataExchangeService : IDataExchangeService
    {
        ///////////////////////////////////////////////////////////////////////

        public Task<string> ConnectAsync(string clientMoniker)
        {
            var container = DataExchangeContainer.Current;
            var session = container.OpenSession(
                OperationContext.Current.GetCallbackChannel<IDataExchangeServiceCallback>(),
                clientMoniker);

            var sid = session != null
                      ? session.Sid
                      : null;

            return Task.FromResult(sid);
        }

        public Task<BoardContract> GetBoardAsync(string sid)
        {
            var document = new BoardContract(
                new MapContract(
                    new NodeContract[2][]
                    {
                        new NodeContract[2]
                        {
                            new NodeContract(0, 0, 0),
                            new NodeContract(1, 0, 0)
                        },
                        new NodeContract[2]
                        {
                            new NodeContract(0, 1, 0),
                            new NodeContract(1, 1, 0)
                        }
                    }),
                null);

            return Task.FromResult(document);
        }

        public Task SetBoardAsync(string sid, BoardContract document)
        {
            return Task.FromResult(null as object);
        }

        public void Disconnect(string sid)
        {
        }
    }
}
